//
//  MoviePlayerExampleViewController.m
//  MoviePlayerExample
//

#import "MoviePlayerExampleViewController.h"

@implementation MoviePlayerExampleViewController

- (void)moviePlayerDidFinish:(NSNotification *)aNote {
	MPMoviePlayerController *player = [aNote object];
	[[NSNotificationCenter defaultCenter]
	 removeObserver:self
	 name:MPMoviePlayerPlaybackDidFinishNotification
	 object:player];
	[player stop];
	[self dismissMoviePlayerViewControllerAnimated];
	
	[playerViewController release];
}

- (void)buttonClick:(id)sender {
	NSString *filePath = [[[NSBundle mainBundle] resourcePath]
						  stringByAppendingPathComponent:@"movie.mov"];
	NSURL *movieURL = [NSURL fileURLWithPath:filePath];
	
	playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
	
	[[NSNotificationCenter defaultCenter]
	 addObserver:self
	 selector:@selector(moviePlayerDidFinish:)
	 name:MPMoviePlayerPlaybackDidFinishNotification
	 object:[playerViewController moviePlayer]];
	[self presentMoviePlayerViewControllerAnimated:playerViewController];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[btn setFrame:CGRectMake(100,110,120,34)];
	[btn setTitle:@"Show Movie" forState:UIControlStateNormal];
	[btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:btn];
}

- (void)dealloc {
    [super dealloc];
}

@end
